package com.wl.lib.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.content.res.Resources
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import androidx.annotation.ColorInt

// Created by wangliang on 2024/5/8.
object StatusBarUtils {
    /**
     * 初始化activity状态栏
     */
    fun initStatusBar(
        activity: Activity?,
        transparentStatusBar: Boolean,
        @ColorInt statusBarColor: Int,
        statusBarMode: StatusBarMode
    ) {
        if (activity == null || activity.window == null) {
            return
        }
        if (transparentStatusBar) {
            setTransparentStatusBar(activity.window)
        }
        setStatusBarColor(activity.window, statusBarColor)
        setStatusBarMode(activity.window, statusBarMode)
    }

    /**
     * 初始化渐变状态栏背景
     */
    fun initDynamicStatusBarBg(window: Window?, statusBarPaddingView: View, statusBarMode: StatusBarMode) {
        if (setTransparentStatusBar(window)) {
            setStatusBarMode(window, statusBarMode)
            statusBarPaddingView.visibility = View.VISIBLE
        } else {
            statusBarPaddingView.visibility = View.GONE
        }
    }

    /**
     * 设置状态栏占位View高度，如果是不支持透明状态栏的手机，则高度为0
     */
    fun setStatusBarPaddingViewHeight(statusPaddingView: View) {
        val params = statusPaddingView.layoutParams as ViewGroup.LayoutParams
        params.height = getStatusBarHeight()
        statusPaddingView.setLayoutParams(params)
    }

    fun getStatusBarHeight(): Int {
        val resources: Resources = ApplicationHelper.getInstance().resources
        var result = 72 // 1920x1280
        val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            result = resources.getDimensionPixelSize(resourceId)
        }
        return result
    }

    /**
     * 设置窗口使用透明浅色状态栏
     */
    fun setLightTransparentStatusBar(window: Window?): Boolean {
        return setTransparentStatusBar(window) && setStatusBarMode(window, StatusBarMode.MODE_LIGHT)
    }

    /**
     * 设置窗口使用透明深色状态栏
     */
    fun setDarkTransparentStatusBar(window: Window?): Boolean {
        return setTransparentStatusBar(window) && setStatusBarMode(window, StatusBarMode.MODE_DARK)
    }

    /**
     * 设置透明状态栏，6.0以上为全透明，6.0以下5.0及以上为半透明，小米魅族5.0及以上为全透
     */
    @SuppressLint("InlinedApi")
    private fun setTransparentStatusBar(window: Window?): Boolean {
        if (window == null) {
            return false
        }

        setStatusBarColor(window, Color.TRANSPARENT)

        window.decorView.systemUiVisibility = (window.decorView.systemUiVisibility
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
        return true
    }

    /**
     * 判断当前window是否设置了透明状态栏
     */
    @SuppressLint("InlinedApi")
    fun isTransparentStatusBarEnabled(window: Window?): Boolean {
        if (window == null) {
            return false
        }
        val uiVisibility = window.decorView.systemUiVisibility
        return (uiVisibility and View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN != 0
                && uiVisibility and View.SYSTEM_UI_FLAG_LAYOUT_STABLE != 0)

    }

    /**
     * 设置状态栏模式，状态栏图标和文字将变为深色/浅色
     */
    fun setStatusBarMode(window: Window?, statusBarMode: StatusBarMode): Boolean {
        if (window == null) {
            return false
        }
        return setAboveMashMallowStatusBarMode(window, statusBarMode == StatusBarMode.MODE_LIGHT)
    }

    /**
     * 设置状态栏颜色
     */
    fun setStatusBarColor(window: Window, @ColorInt bgColor: Int): Boolean {
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = bgColor
        return true
    }

    private fun setAboveMashMallowStatusBarMode(window: Window, darkIcon: Boolean): Boolean {
        val decor = window.decorView
        if (darkIcon) {
            decor.systemUiVisibility = decor.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        } else {
            decor.systemUiVisibility = decor.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
        }
        return true
    }
}

enum class StatusBarMode {
    MODE_LIGHT,
    MODE_DARK
}